package com.zq.it.action.bbs;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import com.zq.it.action.BaseAction;
import com.zq.it.pojo.Page;
import com.zq.it.pojo.Post;
import com.zq.it.pojo.Response;
import com.zq.it.pojo.Type;
import com.zq.it.pojo.User;
import com.zq.it.service.bbs.BbsService;
import com.zq.it.tool.BbsConstants;
import com.zq.it.tool.Constant;
import com.zq.it.tool.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class BbsAction extends BaseAction {

	private static final long serialVersionUID = -1050724637264250855L;
	 
    @Resource
    private BbsService bbsService;
    private Post post;
    private Response resp;
    private User user;
    private String name;
    private Page pageConfig;
    private String content;
    private Type postType;

    public String initData() {
        if (pageConfig == null) {
        	pageConfig = new Page(1);
        }
        Map typeMap = bbsService.findTypes(pageConfig, name);
        getRequest().setAttribute(BbsConstants.ETY_LIST, typeMap.get(BbsConstants.ETY_LIST));
        pageConfig.setUrl(getRequest().getContextPath() + "/bbs_initData.asp");
        pageConfig.setTotalCount((int) typeMap.get(BbsConstants.ETY_TOTALCOUNT));
        pageConfig.getPageCount();
        getRequest().setAttribute(BbsConstants.PAGE, pageConfig);
        return SUCCESS;
    }

    public String myPosts() {
        if (pageConfig == null) {
            pageConfig = new Page(1);
        }
        User user = (User)getHttpSession().getAttribute(Constant.USER);
        Map typeMap = bbsService.findMyPosts("userName", user.getAccount(), pageConfig);
        getRequest().setAttribute(BbsConstants.ETY_LIST, typeMap.get(BbsConstants.ETY_LIST));
        pageConfig.setUrl(getRequest().getContextPath() + "/bbs_myPosts.asp");
        pageConfig.setTotalCount((int) typeMap.get(BbsConstants.ETY_TOTALCOUNT));
        pageConfig.getPageCount();
        getRequest().setAttribute(BbsConstants.PAGE, pageConfig);
        return "myposts";
    }

    public String findRespData() {
        if (pageConfig == null) {
            pageConfig = new Page(1);
        }
        Map typeMap = bbsService.findResps(pageConfig, getId());
        getRequest().setAttribute(BbsConstants.ETY_LIST, typeMap.get(BbsConstants.ETY_LIST));
        pageConfig.setUrl(getRequest().getContextPath() + "/bbs_findRespData.asp?id=" + getId());
        pageConfig.setTotalCount((int) typeMap.get(BbsConstants.ETY_TOTALCOUNT));
        pageConfig.getPageCount();
        getRequest().setAttribute(BbsConstants.PAGE, pageConfig);
        post = (Post) bbsService.getById(getId(), Post.class);
        getRequest().setAttribute(BbsConstants.POST, post);
        return "respData";
    }
    
    public void validateAddPost() {
		if (StringUtil.isEmpty(post.getTitle()))
			addFieldError("title", "标题不能为空！");
		if (StringUtil.isEmpty(content))
			addFieldError("content", "内容不能为空！");
		if (StringUtil.isEmpty(postType.getName()))
			addFieldError("type", "请输入问题类型");
	}

    public void addPost() throws IOException {   
        post.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        post.setContent(content);
        int typeId = postType.getId();
        if (typeId == 0) {
        	typeId = bbsService.addType(postType);
        }
        post.setTypeId(typeId); 
        bbsService.addObject(post);
       print_html_utf8("<script>parent.parent.closeWindow();</script>");
    }

    public String addPostPage() {
        return "input";
    }
    public void findTypeData () {
    	print(JSONArray.fromObject(bbsService.findTypes(),Constant.getJcf(new String[]{"posts"})).toString(),"UTF-8","APPLICATION/JSON");
    }
    public void addResp() throws IOException {
        User user = (User)getHttpSession().getAttribute(BbsConstants.USER);
        if (null == resp) {
            resp = new Response();
        }
        resp.setContent(content);
        resp.setUserName(user.getAccount());
        resp.setResponseTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        bbsService.addObject(resp);
        Post post = (Post) bbsService.getById(resp.getPost().getId(), Post.class);
        post.addRespCount(1);
        bbsService.updaObject(post);
        getResponse().setCharacterEncoding("UTF-8");
        getResponse().getWriter().write("<script>location.href='bbs_findRespData.asp?id="+ resp.getPost().getId() + "';</script>");
    }

    public String deletePost() {
        bbsService.deleteObject(post);
        return SUCCESS;
    }

    public void deleteResp() throws IOException {
        bbsService.deleteObject(resp);
        getResponse().getWriter().write(
                "<script>location.href='bbs_findRespData.asp?id=" + resp.getPost().getId()
                        + "';</script>");
    }
    
    public void getTodayPostsNumber() throws IOException, ParseException {
    	Date tempTime = new Date();
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    	String today = dateFormat.format(tempTime);
		String tomorrow = getMistiming(tempTime, Calendar.DATE, 1, dateFormat);
		String yesterday = getMistiming(tempTime, Calendar.DATE, -1, dateFormat);
		int todayCount = bbsService.getPostsCount(tomorrow, today);
		int yesterCount = bbsService.getPostsCount(today, yesterday);
		JSONObject numbers = new JSONObject();
		numbers.put("today", todayCount);
		numbers.put("yesterday", yesterCount);
		System.out.println(numbers.toString());
    	print_json_utf8(numbers.toString());
    }

    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }

    public Response getResp() {
        return resp;
    }

    public Page getPageConfig() {
		return pageConfig;
	}

	public void setPageConfig(Page pageConfig) {
		this.pageConfig = pageConfig;
	}

	public void setResp(Response resp) {
        this.resp = resp;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

	public Type getPostType() {
		return postType;
	}

	public void setPostType(Type postType) {
		this.postType = postType;
	}
    
    

}
